package com.hk.service.business;

import cn.hutool.core.thread.ThreadUtil;
import com.hk.domain.dto.UserDTO;
import com.hk.enums.OuterErrorEnum;
import com.hk.utils.AssertUtils;
import com.hk.utils.ThreadPoolExecutorMdcWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class LoginBusiness {
    public static void login(UserDTO userDTO) {
        log.info("=====================start======login接口=====================");
        userDTO.setUsername(userDTO.getUsername());
        userDTO.setPassword("zs");
        //断言的方式进行数据校验
        AssertUtils.isNotBlank(userDTO.getUsername(), OuterErrorEnum.PARAM_ISNULL_ERROR_USERNAME);
        log.info("=====================end======login接口=====================");

        //必须使用定义的ThreadPoolExecutorMdcWrapper线程池才能把traceId传递到子线程
        ThreadPoolExecutorMdcWrapper poolExecutorMdcWrapper = new ThreadPoolExecutorMdcWrapper(
                1,
                1,
                1,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100));

        poolExecutorMdcWrapper.execute(() -> {
            log.info("=====================start======thread 1=====================");
            log.info("=====================end======thread 1=====================");
        });

        poolExecutorMdcWrapper.execute(() -> {
            log.info("=====================start======thread 2=====================");
            log.info("=====================end======thread 2=====================");
        });

    }
}
